import {createRouter, createWebHistory} from 'vue-router'
import Content from "@/components/SearchContent/Content.vue";
import {useUserStore} from "@/stores/UserStore.js";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes: [
		{
			path: '/',
			redirect: '/login'
		},
		{
			path: '/pl',
			name: 'userInfo',
			component: () => import('@/views/PersonalInfo.vue'),
		},
		{
			path: '/setting',
			name: 'setting',
			component: () => import('@/views/setting.vue')
		},
		{
			path: '/phone',
			name: 'phone',
			component: () => import("@/views/PhonePage.vue")
		},
		{
			path: '/steam/:id?/:status?',
			name: 'steam',
			component: () => import("@/views/Steam.vue"),
			props: true
		},
		{
			path: '/shop',
			name: 'shop',
			component: () => import("@/views/Shop.vue")
		},
		{
			path: '/plCenter',
			name: 'plCenter',
			component: () => import("@/views/PlCenter.vue")
		},
		{
			path: '/talkBot',
			name: 'talkBot',
			component: () => import("@/views/DeepSeek.vue")
		},
		{
			path: '/GameDt/:id/:have?/:gid?',
			name: 'gameDt',
			component: () => import("@/views/GameDetails.vue"),
			props: true
		},
		{
			path: '/gdn/:content/:name/:id',
			name: 'dn',
			component: () => import('@/views/GameDescription.vue'),
			props: true
		},
		{
			path: '/search',
			name: 'search',
			component: () => import("@/views/Search.vue")
		}, {
			path: '/login',
			name: 'login',
			component: () => import('@/views/login.vue')
		},
		{
			path: '/Recommend',
			name: 'Recommend',
			component: () => import("@/views/Recommend.vue")
		},
		{
			path: '/Dynamical',
			name: 'Dynamical',
			component: () => import("@/views/Dynamical.vue")
		},
		{
			path: '/Release',
			name: 'Release',
			component: () => import("@/views/Release.vue")
		},
		{
			path: '/Collection',
			name: 'Collection',
			component: () => import("@/views/Collection.vue")
		},
		{
			path: '/Publish',
			name: 'Publish',
			component: () => import("@/views/Publish.vue")
		}, {
			path: '/SearchContent',
			name: 'SearchContent',
			component: () => import("@/views/SearchContent.vue"),
			children: [
				{
					path: '',
					name: 'Content',
					component: Content // 默认显示的组件
				},
				{
					path: '/Content',
					name: 'ContentRoute', // 默认显示的组件
					component: () => import("@/components/SearchContent/Content.vue")
				},
				{
					path: '/Game',
					name: 'GameRoute', // 默认显示的组件
					component: () => import("@/components/SearchContent/Game.vue")
				}],
		},
		{
			path: '/msgLogin',
			name: 'msgLogin',
			component: () => import("@/views/MSGLogin.vue")
		},
		{
			path: '/text',
			name: 'text',
			component: () => import("@/components/SearchContent/Sh.vue")
		},
		{
			path: '/sc',
			name: 'sc',
			component: () => import("@/views/SC.vue")
		},
		{
			path: "/PinLun",
			name: "PinLun",
			component: () => import('../views/PinLun.vue')
		},
		{
			path: "/index/:id",
			name: "index",
			component: () => import('../views/index.vue')
		}, {
			path: "/sosuo",
			name: 'sosuo',
			component: () => import('../views/sosuo.vue')
		}, {
			path: "/ShoYe",
			name: 'ShoYe',
			component: () => import('../views/ShoYe.vue')
		},
		{
			path: '/home',
			name: 'home',
			// route level code-splitting
			// this generates a separate chunk (About.[hash].js) for this route
			// which is lazy-loaded when the route is visited.
			component: () => import('../views/RenWView.vue'),
		},
		{
			path: '/chengJiu',
			name: 'chengJiu',
			component: () => import('../views/ChengJiu.vue')
		}
	],
})

router.beforeEach((to, from, next) => {
	const userStore = useUserStore();
	const isLoggedIn = !!userStore.getUserInfo?.id; // 判断是否已登录

	// ✅ 已登录用户访问登录页面时，重定向到用户中心
	if (isLoggedIn && (to.path === '/login' || to.path === '/msgLogin')) {
		next('/ShoYe');
	}
	// ✅ 未登录用户访问非登录页面时，重定向到登录页面
	else if (!isLoggedIn && to.path !== '/login' && to.path !== '/msgLogin') {
		next('/login');
	}
	// ✅ 其他情况正常放行
	else {
		next();
	}
});

export default router